package io.renren.modules.pinkao.service.impl;

import io.renren.modules.pinkao.entity.PinKaoTeamAndParent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.pinkao.dao.PinKaoCourseDao;
import io.renren.modules.pinkao.entity.PinKaoCourseEntity;
import io.renren.modules.pinkao.service.PinKaoCourseService;


@Service("pinKaoCourseService")
public class PinKaoCourseServiceImpl extends ServiceImpl<PinKaoCourseDao, PinKaoCourseEntity> implements PinKaoCourseService {

    @Autowired
    PinKaoCourseDao pinKaoCourseDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<PinKaoCourseEntity> page = this.selectPage(
                new Query<PinKaoCourseEntity>(params).getPage(),
                new EntityWrapper<PinKaoCourseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<PinKaoTeamAndParent> getPinKaoTeamAndParentList(Integer pinKaoId) throws UnsupportedEncodingException {
        List<PinKaoTeamAndParent> teams=pinKaoCourseDao.getPinKaoTeamList(pinKaoId);
        List<PinKaoTeamAndParent> users=pinKaoCourseDao.getPinKaoUserList(pinKaoId);
        for(PinKaoTeamAndParent pinKaoTeamAndParent:teams){
                Integer teamId=pinKaoTeamAndParent.getId()+10000000;
            for(PinKaoTeamAndParent p:users){
                if(p.getPinKaoTeamId()==pinKaoTeamAndParent.getId()){
                    p.setParentId(teamId);
                    if(p.getPinKaoNickNameB()!=null){
                        p.setPinKaoNickName(new String(p.getPinKaoNickNameB(),"utf-8"));
                    }
                }
            }

            pinKaoTeamAndParent.setId(teamId);
            if(pinKaoTeamAndParent.getNickNamesB()!=null)
                pinKaoTeamAndParent.setNickNames(new String(pinKaoTeamAndParent.getNickNamesB(),"utf-8"));
        }
        teams.addAll(users);
        return teams;
    }

}
